package com.xcity.tools.http.impl;

import java.lang.reflect.Method;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcity.game.battle.pve.template.StageTemplate;
import com.xcity.game.item.drop.Drop;
import com.xcity.game.item.drop.impl.ExpDrop;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.item.drop.impl.ItemDrop;
import com.xcity.tools.http.XLS2JSONFilter;

public class StageTemplateFilter implements XLS2JSONFilter<StageTemplate> {

	@Override
	public Object doFilter(StageTemplate template, String field, Method getter) throws Exception {
		if ("rewards".equals(field)) {
			List<Drop> drops = template.getRewardsDrop();
			JSONObject wrapped = new JSONObject(true);
			for (Drop drop : drops) {
				if (drop instanceof GoldDrop) {
					wrapped.put("gold", ((GoldDrop) drop).getGold());
				} else if (drop instanceof ExpDrop) {
					wrapped.put("exp", ((ExpDrop) drop).getExp());
				} else if (drop instanceof ItemDrop) {
					ItemDrop item = (ItemDrop) drop;
					JSONArray items = wrapped.getJSONArray("items");
					if (items == null) {
						items = new JSONArray();
						wrapped.put("items", items);
					}
					items.add(item);
				}
			}
			return wrapped;
		}
		return getter.invoke(template);
	}

}
